package com.ruoyi.controller.achievements;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.service.achievements.AcvDepManageService;
import com.ruoyi.service.achievements.AcvEmpManageService;
import com.ruoyi.service.achievements.AcvTemplateManageService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 绩效人员管理
 * @date 2020/8/21 16:33
 */
@Controller
@RequestMapping("/acvEmpManage")
public class AcvEmpManageController extends BaseController {

    @Autowired
    AcvDepManageService acvDepManageService;

    @Autowired
    AcvTemplateManageService acvTemplateManageService;

    @Autowired
    AcvEmpManageService acvEmpManageService;

    @Autowired
    ISysUserService sysUserService;

    private String prefix = "achievements/empmanage";

    @GetMapping()
    public String acvEmpManage(ModelMap mmap) {
        mmap.put("secondaryDeptList", acvDepManageService.findSecondaryDept());

        return prefix + "/acvEmpManage";
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Map> list(@RequestParam Map map) {
        return acvEmpManageService.list(map);
    }

    @GetMapping("/toAddExamine")
    public String toAddExamine(ModelMap mmap) {
        mmap.put("secondaryDeptList", acvDepManageService.findSecondaryDept());

        mmap.put("jobList", acvDepManageService.findJobList());
        return prefix + "/addScoring";
    }

    @RequestMapping("/addExamine")
    @ResponseBody
    public AjaxResult addExamine(@RequestParam Map map) {
        String loginName = map.get("loginName").toString();
        if (acvEmpManageService.findCountByLoginName(loginName) > 0) {
            return AjaxResult.warn("登录用户已存在");
        }
        return toAjax(acvEmpManageService.addExamine(map));
    }

    @GetMapping("/add")
    public String add(Long userId, ModelMap mmap) {
        SysUser sysUser = sysUserService.selectUserById(userId);
        mmap.put("user", sysUser);
        mmap.put("secondaryDeptList", acvDepManageService.findSecondaryDept());
        mmap.put("jobList", acvDepManageService.findJobList());
        return prefix + "/add";
    }

    @GetMapping("/edit")
    public String edit(String userId, int type, ModelMap mmap) {
        if (type == 0) {
            //修改打分人
            Map sysUser = acvEmpManageService.findUserInfo(userId);
            mmap.put("secondaryDeptList", acvDepManageService.findSecondaryDept());
            mmap.put("user", sysUser);
            return prefix + "/editScoring";
        } else {
            //修改考核人
            Map map = acvEmpManageService.findExamineeByUserId(userId);
            mmap.put("user", map);
            mmap.put("secondaryDeptList", acvDepManageService.findSecondaryDept());
            mmap.put("jobList", acvDepManageService.findJobList());
            mmap.put("superList", acvDepManageService.findSuperList());
            return prefix + "/edit";
        }
    }

    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map map) {
        return toAjax(acvEmpManageService.add(map));
    }

    @RequestMapping("/editScoring")
    @ResponseBody
    public AjaxResult editScoring(@RequestParam Map map) {
        return toAjax(acvEmpManageService.editScoring(map));
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(acvEmpManageService.edit(map));
    }

    @RequestMapping("/remove/{userId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("userId") String userId) {
        return toAjax(acvEmpManageService.remove(userId));
    }

    @RequestMapping("/getTertiaryDept")
    @ResponseBody
    public List<Map> getTertiaryDept(int deptId) {
        return acvDepManageService.findTertiaryDept(deptId);
    }

    @RequestMapping("/getJobByTertiaryDeptId")
    @ResponseBody
    public List<Map> getJobByTertiaryDeptId(String tertiaryDeptId) {
        return acvDepManageService.getJobByTertiaryDeptId(tertiaryDeptId);
    }

    @RequestMapping("/removeScoringPerson")
    @ResponseBody
    public AjaxResult removeScoringPerson(int superId) {

        //判断打分人员是否正在进行打分
        if (acvEmpManageService.findEmpCountBySuperUserId(superId) > 0) {
            return new AjaxResult(AjaxResult.Type.ERROR, "该打分人员正在进行考核打分,请先提交!");
        }

        //判断打分人员下是否有考核人员
        if (acvEmpManageService.findExamineeCountBySuperUserId(superId) > 0) {
            return new AjaxResult(AjaxResult.Type.ERROR, "该打分人员下存在考核人员,请先删除考核人员!");
        }
        return toAjax(acvEmpManageService.updateScoreUserInvalid(superId));
    }

    @GetMapping("/toTwoWay")
    public String toTwoWay(String userId, ModelMap mmap) {
        mmap.put("userId", userId);
        //取打分人列表
        List<Map<String, Object>> userList = acvEmpManageService.findScorerList(userId);
        String userDeptList = "";
        for (Map map : userList) {
            userDeptList += map.get("nameDept") + ",";
        }

        Map map = acvEmpManageService.findExamineeByUserId(userId);
        mmap.put("userDeptListStr", userDeptList.substring(0, userDeptList.length() - 1));
        mmap.put("userDeptList", userList);
        mmap.put("user", map);

        String name = map.get("examineeName").toString();
        String badge = map.get("badge").toString();
        mmap.put("examineeName", name);
        mmap.put("badge", badge);
        //通过姓名和工号查找占比
        Map map1 = acvEmpManageService.findTwoWay(name, badge);
        if (map1 == null) {
            map1 = new HashMap();
            map1.put("function_dept_id", -1);
            map1.put("function_proportion", "");
            map1.put("pro_dept_id", -1);
            map1.put("pro_proportion", "");
        }
        mmap.put("twoWay", map1);
        return prefix + "/toTwoWay";
    }

    @RequestMapping("/twoWay")
    @ResponseBody
    public AjaxResult twoWay(@RequestParam Map map) {
        String name = map.get("examineeName").toString();
        String badge = map.get("badge").toString();
        if (acvEmpManageService.findTwoWayCount(name, badge) > 0) {
            acvEmpManageService.deleteTwoWay(name, badge);
        }
        return toAjax(acvEmpManageService.insertTwoWay(map));
    }
}
